import { CopyControllerBase } from "./CopyControllerBase";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SAttribute } from "../../../../../Message/Public/SAttribute";
import { ECopyType } from "../../../../../Message/Public/ECopyType";
import { SMapPassPointList } from "../../../../../Message/Public/SMapPassPointList";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { AutoFightRule } from "../../../rules/AutoFightRule";
import { GameProxy } from "../../../mvc/GameProxy";
import { CopyConfig } from "../../../resource/CopyConfig";
import { MapFileUtil } from "../../../scene/map/MapFileUtil";
import { MsgManager } from "../../../manager/MsgManager";
import { GameController } from "../../../mvc/GameController";
import { RolePlayer } from "../../../scene/player/entity/RolePlayer";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { Language } from "../../../../../extend/language/Language";
import { IView } from "../../../../mvc/interfaces/IView";
import { SeventyCopyView } from "../seventyCopy/SeventyCopyView";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
type int = number;
//class SeventyCopyController
    
    export  class SeventyCopyController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        /* internal  */onSeventyCopyReviveTimeChange(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SAttribute;
            cache.copy.seventyCopyReviveTime = loc1.value;
            return;
        }

        /* internal  */onSeventyPassPointInfoChange(arg1: MessageBlock): void
        {
            var loc1=null;
            var loc2=null;
            if (cache.copy.isInCopyType(ECopyType._ECopySeventy)) 
            {
                loc1 = arg1.messageBase as SMapPassPointList;
                var loc3=0;
                var loc4=loc1.passPoints;
                for(loc2 of loc4) 
                {
                    if (!loc2.show) 
                    {
                        continue;
                    }
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_ClickCopyExit));
            }
            return;
        }

        /* internal  */onReceiveRemainTime(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SAttribute;
            this.seventyCopyView.updateInfoPanelTime(loc1.value);
            return;
        }

        /* internal  */onReceiveNextPassId(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SAttribute;
            cache.copy.seventyCopyNextfloor = loc1.value;
            cache.copy.seventyCopyNextfloorName = loc1.valueStr;
            if (!AutoFightRule.isAutoFight()) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyQuitCopy));
            }
            return;
        }

        /* internal  */onSeventyCopyRevive(arg1: DataEvent): void
        {
            GameProxy.copy.moveToPassPoint(arg1.data as int);
            return;
        }

        /* internal  */onSeventyCopyChangeScene(arg1: DataEvent): void
        {
            cache.copy.seventyCopyNextfloor = -1;
            var loc1=CopyConfig.instance.getSeventyCopyContext(MapFileUtil.mapID);
            if (loc1 != "") 
            {
                MsgManager.showTaskTarget(loc1);
                GameController.chat.addTypeRumorMsg(loc1, []);
            }
            var loc2=cache.copy.isInSeventyCopyWhichFloor();
            if (loc2 == 1 && !cache.copy.isInKingCopy()) 
            {
                this.seventyCopyView.hideInfoPanel();
            }
            else if (loc2 == 2 && !cache.copy.isInKingCopy()) 
            {
                if (!CopyConfig.instance.isReviveLimit(MapFileUtil.mapID)) 
                {
                    flash.utils.setTimeout(this.showInfoPanel, 500);
                }
            }
            return;
        }

        /* internal  */showInfoPanel(): void
        {
            if (cache.copy.isInSeventyCopyWhichFloor() == 2) 
            {
                this.seventyCopyView.showInfoPanel();
            }
            return;
        }

        /* internal  */onHideTargetPoint(): void
        {
            RolePlayer.instance.removeTargetTilePoint();
            return;
        }

        /* internal  */onLeaveSeventyCopySecondFloorHandler(arg1: DataEvent): void
        {
            var loc1=0;
            if (AutoFightRule.isAutoFight()) 
            {
                this.onLeaveSeventyCopyConfirm(Alert.OK);
            }
            else 
            {
                Alert.showCloseBtn = true;
                Alert.calcelLabel = Language.getString(60366);
                Alert.buttonWidth = 70;
                if (cache.copy.seventyCopyNextfloor > 0) 
                {
                    Alert.okLabel = Language.getStringByParam(60453, cache.copy.seventyCopyNextfloorName);
                    loc1 = Alert.OK | Alert.CANCEL;
                }
                else 
                {
                    loc1 = Alert.CANCEL;
                }
                Alert.show(Language.getString(60367), null, loc1, null, this.onLeaveSeventyCopyConfirm);
            }
            return;
        }

        /* internal  */onLeaveSeventyCopyConfirm(arg1: int): void
        {
            if (arg1 != Alert.OK) 
            {
                if (arg1 == Alert.CANCEL) 
                {
                    GameProxy.copy.moveToPassPoint(0);
                }
            }
            else 
            {
                GameProxy.copy.moveToPassPoint(cache.copy.seventyCopyNextfloor);
            }
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            this.seventyCopyView.hideInfoPanel();
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._seventyCopyView) 
            {
                this._seventyCopyView = new SeventyCopyView();
            }
            return this._seventyCopyView;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicSeventyCopyReviveTime, this.onSeventyCopyReviveTimeChange);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicPassPointInfo, this.onSeventyPassPointInfoChange);
            return;
        }

        protected /* override */ addListener(): void
        {
            Dispatcher.addEventListener(EventName.ChangeScene, this.onSeventyCopyChangeScene);
            Dispatcher.addEventListener(EventName.CopySeventyCopyRevive, this.onSeventyCopyRevive);
            Dispatcher.addEventListener(EventName.Copy_LeaveSeventyCopySecondFloor, this.onLeaveSeventyCopySecondFloorHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicSeventyCopyRemainTime, this.onReceiveRemainTime);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicSeventyCopyNextPassId, this.onReceiveNextPassId);
            return;
        }

        protected /* override */ removeListener(): void
        {
            Dispatcher.removeEventListener(EventName.ChangeScene, this.onSeventyCopyChangeScene);
            Dispatcher.removeEventListener(EventName.CopySeventyCopyRevive, this.onSeventyCopyRevive);
            Dispatcher.removeEventListener(EventName.Copy_LeaveSeventyCopySecondFloor, this.onLeaveSeventyCopySecondFloorHandler);
            NetDispatcher.removeCmdListener(EPublicCommand._ECmdPublicSeventyCopyRemainTime, this.onReceiveRemainTime);
            NetDispatcher.removeCmdListener(EPublicCommand._ECmdPublicSeventyCopyNextPassId, this.onReceiveNextPassId);
            return;
        }

        public /* override */ stageResize(): void
        {
            this.seventyCopyView.stageResize();
            return;
        }

        public get seventyCopyView(): SeventyCopyView
        {
            if (!this._seventyCopyView) 
            {
                this.initView();
            }
            return this._seventyCopyView;
        }

        /* internal */ /* var */_seventyCopyView: SeventyCopyView;
    }
